<template>
  <div class="main">
    <div class="main-title">盘点计划</div>
    <div class="main-content">
      <div class="content-wrapper">
        <div class="tools-wrapper">
          <el-button type="primary" v-if="hasPermission('1004010102')" @click="add">添加</el-button>
        </div>
        <div class="table-wrapper">
          <el-table :data="tableData" border stripe v-loading="$store.state.isLoading">
            <el-table-column label="序号" width="60" fixed="left" align="center">
              <template slot-scope="scope">
                {{(page-1)*pageSize + scope.$index + 1}}
              </template>
            </el-table-column>
            <el-table-column label="库存截止时间" prop="createTime" align="center"></el-table-column>
            <el-table-column label="状态" align="center">
              <template slot-scope="scope">
                {{checkStatusTypes.Types[scope.row.checkStatus]}}
              </template>
            </el-table-column>
            <el-table-column label="创建人" prop="creator" align="center"></el-table-column>
            <el-table-column label="盘点说明" prop="checkExplain" align="center"></el-table-column>
            <el-table-column fixed="right" label="操作" width="240"  align="center">
              <template slot-scope="scope">
                <el-button type="text" size="mini" v-if="hasPermission('1004010103') && scope.row.checkStatus == 1" @click="exportInfo(scope)">导出</el-button>
                <el-button type="text" size="mini" v-if="hasPermission('1004010104') && scope.row.checkStatus == 1" @click="edit(scope)">填写报告</el-button>
                <el-button type="text" size="mini" v-if="hasPermission('1004010105') && scope.row.checkStatus == 2" @click="view(scope)">查看报告</el-button>
              </template>
            </el-table-column>
          </el-table>
          <v-pagination v-if="total" :pg="page" :size="pageSize" :total="total" :views="5" @page-change="pageChange" @size-change="sizeChange"></v-pagination>
        </div>
      </div>
    </div>
    <!-- 添加盘点计划 -->
    <el-dialog title="添加盘点计划" width="520px" :visible.sync="checkDialog" :close-on-click-modal="false">
      <el-form :model="checkForm" label-width="80px">
        <el-form-item label="盘点说明">
          <div class="textarea-box">
            <el-input class="textarea" v-model="checkForm.checkExplain" :rows="4" type="textarea" :maxlength="1000"></el-input>
            <span class="desc">{{checkForm.checkExplain.length}}/1000字</span>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save" :loading="loading">保存</el-button>
        <el-button type="default" @click="checkDialog = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import QS from 'qs'
import { sign } from '@/utils/sign'
import { checkStatusTypes } from '@/data/enum'
import { formatDate } from '@/utils/date.js'
export default {
  data () {
    return {
      checkStatusTypes,
      tableData: [],
      page: Number(this.$route.query.pg) || 1,
      pageSize: Number(this.$route.query.size) || 10,
      total: 0,
      checkDialog: false,
      checkForm: {
        checkExplain: ''
      },
      loading: false
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    // 获取盘点计划信息
    getData () {
      this.$store.commit('setLoading', true)
      this.$http.post(this.$api + '/stockCheckPlan/list', {
        checkPlanNo: '',
        page: this.page,
        pageSize: this.pageSize
      }).then(res => {
        if (res.responseCode === '0000') {
          res.data.list.forEach(item => {
            item.createTime = formatDate(new Date(item.createTime), 'yyyy-MM-dd hh:mm:ss')
          })
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error({
            duration: this.$store.state.duration,
            message: res.responseMsg
          })
        }
        this.$store.commit('setLoading', false)
      }).catch(err => {
        console.error(err)
        this.$store.commit('setLoading', false)
      })
    },
    // 添加盘点计划
    add () {
      this.checkDialog = true
      this.$nextTick(() => {
        this.checkForm.checkExplain = ''
      })
    },
    // 保存
    save () {
      if (!this.loading) {
        this.loading = true
        this.$http.post(this.$api + '/stockCheckPlan/add', {
          type: 'pc',
          checkExplain: this.checkForm.checkExplain || ''
        }).then(res => {
          if (res.responseCode === '0000') {
            this.$message.success({
              duration: this.$store.state.duration,
              message: res.responseMsg,
              onClose: () => {
                this.loading = false
                this.checkDialog = false
                this.getData()
              }
            })
          } else {
            this.$message.error({
              duration: this.$store.state.duration,
              message: res.responseMsg,
              onClose: () => {
                this.loading = false
              }
            })
          }
        }).catch(err => {
          this.loading = false
          console.error(err)
        })
      }
    },
    // 分页
    sizeChange (size) {
      this.pageSize = size
      this.getData()
    },
    pageChange (page) {
      this.page = page
      this.getData()
    },
    search () {
      this.page = 1
      this.getData()
    },
    // 导出
    exportInfo (scope) {
      let param = {
        checkPlanId: scope.row.checkPlanId,
        token: this.$store.state.token
      }
      let url = this.$api + '/stockCheckPlan/exportExcel?' + QS.stringify(sign(param))
      window.location.href = url
    },
    // 填写报告
    edit (scope) {
      this.$router.push({
        path: '/stock/editReport',
        query: {
          checkPlanId: scope.row.checkPlanId,
          pg: this.page,
          size: this.pageSize
        }
      })
    },
    // 查看报告
    view (scope) {
      this.$router.push({
        path: '/stock/viewReport',
        query: {
          checkPlanId: scope.row.checkPlanId,
          pg: this.page,
          size: this.pageSize
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.textarea-box {
  font-size: 0;
  .textarea {
    display: inline-block;
    width: 300px;
  }
  .desc {
    margin-left: 10px;
    vertical-align: bottom;
    font-size: 12px;
    line-height: 16px;
    color: #666;
  }
}
</style>
